Skip to contentMethod: lambda$addAssertionToDescriptor$3(AxiomDescriptor, Assertion, URI)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.annotations.SequenceType;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
23: import cz.cvut.kbss.jopa.model.metamodel.CollectionType;
24: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
25: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
26: import cz.cvut.kbss.jopa.model.metamodel.ListAttribute;
27: import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute;
28: import cz.cvut.kbss.jopa.model.metamodel.PropertiesSpecification;
29: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
30: import cz.cvut.kbss.jopa.sessions.util.LoadingParameters;
31: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
32: import cz.cvut.kbss.ontodriver.model.Assertion;
33: import cz.cvut.kbss.ontodriver.model.Axiom;
34: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
35: import cz.cvut.kbss.ontodriver.model.NamedResource;
36: import cz.cvut.kbss.ontodriver.model.Value;
37:
38: import java.net.URI;
39: import java.util.Set;
40:
41: import static cz.cvut.kbss.ontodriver.model.Assertion.createAnnotationPropertyAssertion;
42: import static cz.cvut.kbss.ontodriver.model.Assertion.createDataPropertyAssertion;
43: import static cz.cvut.kbss.ontodriver.model.Assertion.createObjectPropertyAssertion;
44:
45: class AxiomDescriptorFactory {
46:
47: AxiomDescriptor createForEntityLoading(LoadingParameters<?> loadingParams, EntityType<?> et) {
48: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(loadingParams.getIdentifier()));
49: loadingParams.getDescriptor().getContexts().forEach(descriptor::addSubjectContext);
50: descriptor.addAssertion(Assertion.createClassAssertion(false));
51: addForTypes(loadingParams, et, descriptor);
52: addForProperties(loadingParams, et, descriptor);
53: for (Attribute<?, ?> att : et.getAttributes()) {
54: final Assertion a = createAssertion(att, loadingParams.getDescriptor().getAttributeDescriptor(att));
55: addAssertionToDescriptor(loadingParams.getDescriptor(), att, descriptor, a);
56: }
57: return descriptor;
58: }
59:
60: private void addForTypes(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
61: final TypesSpecification<?, ?> types = et.getTypes();
62: if (types != null) {
63: final Descriptor entityDesc = loadingParams.getDescriptor();
64: final Assertion typesAssertion =
65: Assertion.createClassAssertion(includeInferred(types, entityDesc.getAttributeDescriptor(types)));
66: if (descriptor.containsAssertion(typesAssertion) && !entityDesc.getAttributeContexts(types).isEmpty()) {
67: // If the types are non-inferred and have context, we need to merge it with the subject context
68: entityDesc.getAttributeContexts(types).forEach(c -> descriptor.addAssertionContext(typesAssertion, c));
69: entityDesc.getContexts().forEach(c -> descriptor.addAssertionContext(typesAssertion, c));
70: } else {
71: addAssertionToDescriptor(entityDesc, types, descriptor, typesAssertion);
72: }
73: }
74: }
75:
76: private void addAssertionToDescriptor(Descriptor entityDescriptor, FieldSpecification<?, ?> att,
77: final AxiomDescriptor descriptor, final Assertion assertion) {
78: descriptor.addAssertion(assertion);
79: final Set<URI> attContexts = entityDescriptor.getAttributeContexts(att);
80: if (attContexts.isEmpty() && !entityDescriptor.getContexts().isEmpty()) {
81: descriptor.addAssertionContext(assertion, null);
82: } else {
83: attContexts.stream().filter(ctx -> !entityDescriptor.getContexts().contains(ctx))
84: .forEach(ctx -> descriptor.addAssertionContext(assertion, ctx));
85: }
86: }
87:
88: private void addForProperties(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
89: final PropertiesSpecification<?, ?, ?, ?> props = et.getProperties();
90: if (props != null) {
91: final Descriptor entityDesc = loadingParams.getDescriptor();
92: final Assertion propsAssertion = Assertion.createUnspecifiedPropertyAssertion(
93: includeInferred(props, entityDesc.getAttributeDescriptor(props)));
94: addAssertionToDescriptor(entityDesc, props, descriptor, propsAssertion);
95: }
96: }
97:
98: private static Assertion createAssertion(Attribute<?, ?> att, Descriptor descriptor) {
99: assert att != null;
100: switch (att.getPersistentAttributeType()) {
101: case OBJECT:
102: return createObjectPropertyAssertion(att.getIRI().toURI(), includeInferred(att, descriptor));
103: case DATA:
104: if (isReferencedList(att)) {
105: // If the attribute is a referenced list containing data property values, it is mapped as a data property
106: // However, the referenced list nodes themselves are resources (individuals) and thus have to be
107: // referenced via an object property
108: return createObjectPropertyAssertion(att.getIRI().toURI(), includeInferred(att, descriptor));
109: }
110: if (withLanguage(att, descriptor)) {
111: return createDataPropertyAssertion(att.getIRI().toURI(), language(att, descriptor),
112: includeInferred(att, descriptor));
113: } else {
114: return createDataPropertyAssertion(att.getIRI().toURI(), includeInferred(att, descriptor));
115: }
116: case ANNOTATION:
117: if (withLanguage(att, descriptor)) {
118: return createAnnotationPropertyAssertion(att.getIRI().toURI(), language(att, descriptor),
119: includeInferred(att, descriptor));
120: } else {
121: return createAnnotationPropertyAssertion(att.getIRI().toURI(), includeInferred(att, descriptor));
122: }
123: default:
124: throw new IllegalArgumentException(
125: "Illegal persistent attribute type " + att.getPersistentAttributeType());
126: }
127: }
128:
129: private static boolean isReferencedList(Attribute<?, ?> att) {
130: return att.isCollection() && ((PluralAttribute<?, ?, ?>) att).getCollectionType() == CollectionType.LIST &&
131: ((ListAttribute<?, ?>) att).getSequenceType() == SequenceType.referenced;
132: }
133:
134: private static boolean withLanguage(Attribute<?, ?> att, Descriptor descriptor) {
135: return descriptor.hasLanguage() || att.hasLanguage();
136: }
137:
138: private static String language(Attribute<?, ?> att, Descriptor descriptor) {
139: return descriptor.hasLanguage() ? descriptor.getLanguage() : att.getLanguage();
140: }
141:
142: private static boolean includeInferred(FieldSpecification<?, ?> att, Descriptor descriptor) {
143: return att.isInferred() && descriptor.includeInferred();
144: }
145:
146: /**
147: * Creates an axiom representing a class assertion.
148: * <p>
149: * This axiom can be used to load a reference to an individual with the correct type, without any other attributes.
150: *
151: * @param identifier Individual identifier
152: * @param et Entity type. Type IRI is extracted from it
153: * @return {@code Axiom}
154: */
155: Axiom<NamedResource> createForReferenceLoading(URI identifier, EntityType<?> et) {
156: return new AxiomImpl<>(NamedResource.create(identifier), Assertion.createClassAssertion(false),
157: new Value<>(NamedResource.create(et.getIRI().toString())));
158: }
159:
160: AxiomDescriptor createForFieldLoading(URI identifier, FieldSpecification<?, ?> fieldSpec,
161: Descriptor entityDescriptor, EntityType<?> et) {
162: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(identifier));
163: entityDescriptor.getContexts().forEach(descriptor::addSubjectContext);
164: final Assertion assertion;
165: if (et.getTypes() != null && fieldSpec.equals(et.getTypes())) {
166: assertion = Assertion.createClassAssertion(
167: includeInferred(et.getTypes(), entityDescriptor.getAttributeDescriptor(et.getTypes())));
168: } else if (et.getProperties() != null && fieldSpec.equals(et.getProperties())) {
169: assertion = Assertion.createUnspecifiedPropertyAssertion(
170: includeInferred(et.getProperties(), entityDescriptor.getAttributeDescriptor(et.getProperties())));
171: } else {
172: assertion =
173: createAssertion((Attribute<?, ?>) fieldSpec, entityDescriptor.getAttributeDescriptor(fieldSpec));
174: }
175: addAssertionToDescriptor(entityDescriptor, fieldSpec, descriptor, assertion);
176: return descriptor;
177: }
178: }